﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace GPSTrackingWebsiteManager.Manager.Pages
{
    public partial class GPSTypes : ClassManager.SessionManagerPage
    {
        private void GetAllGPSTypes()
        {
            GPSTypeGrid.DataSource = GPSTrackingDBV2.Data.DataRepository.GpsTypeProvider.GetAll();
        }        

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GetAllGPSTypes();

                GridSortExpression expression = new GridSortExpression();
                expression.FieldName = GPSTrackingDBV2.Entities.GpsTypeColumn.Name.ToString();
                expression.SetSortOrder("Ascending");
                this.GPSTypeGrid.MasterTableView.SortExpressions.AddSortExpression(expression);   
            }
        }

        protected void GPSTypeGrid_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            GetAllGPSTypes();
            GPSTypeGrid.Height = Unit.Parse(HiddenFieldHeight.Value.ToString());
        }

        protected void GPSTypeGrid_InsertCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {

            try
            {
                GPSTrackingDBV2.Entities.GpsType GpsTypeEntity = ClassManager.InvokeEntityManager.Invoke_GpsType_Entity(e);
                GpsTypeEntity.GpsTypeId = GPSTrackingDBV2Custom.Data.DataRepository.GpsTypeProvider.GetNextID();


                GridEditFormInsertItem InsertItem = e.Item as GridEditFormInsertItem;
                RadAsyncUpload RadUpload = InsertItem["Image"].FindControl("RadUpload1") as RadAsyncUpload;
                GpsTypeEntity.Image = RadUpload.UploadedFiles[0].FileName;
                RadUpload.UploadedFiles[0].SaveAs(Server.MapPath("~/Template/Icons/GPSType/" + GpsTypeEntity.Image));

                GPSTrackingDBV2.Data.DataRepository.GpsTypeProvider.Insert(GpsTypeEntity);
            }
            catch (Exception ex)
            {
                HiddenFieldError.Value = ClassManager.CustomExceptionManager.Generate_Error(ex.Message);
                showErrorMessage("No se puede insertar la información.");
            }  

        }

        protected void GPSTypeGrid_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            try
            {
                GPSTrackingDBV2.Entities.GpsType GpsTypeEntity = ClassManager.InvokeEntityManager.Invoke_GpsType_Entity(e);  

                GridEditFormItem EditItem = e.Item as GridEditFormItem;
                RadAsyncUpload RadUpload = EditItem["Image"].FindControl("RadUpload1") as RadAsyncUpload;
                if (RadUpload.UploadedFiles.Count > 0)
                {
                    GpsTypeEntity.Image = RadUpload.UploadedFiles[0].FileName;
                    RadUpload.UploadedFiles[0].SaveAs(Server.MapPath("~/Template/Icons/GPSType/" + GpsTypeEntity.Image));
                }
                else
                {
                    GpsTypeEntity.Image = RadUpload.ToolTip;
                }

                GPSTrackingDBV2.Data.DataRepository.GpsTypeProvider.Update(GpsTypeEntity);
            }
            catch (Exception ex)
            {
                HiddenFieldError.Value = ClassManager.CustomExceptionManager.Generate_Error(ex.Message);
                showErrorMessage("No se puede actualizar la información.");
            }
        }

        protected void GPSTypeGrid_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            try
            {
                GPSTrackingDBV2.Entities.GpsType GpsTypeEntity = ClassManager.InvokeEntityManager.Invoke_GpsType_Entity(e);

                System.IO.File.Delete(Server.MapPath("~/Template/Icons/GPSType/" + GpsTypeEntity.Image));

                GPSTrackingDBV2.Data.DataRepository.GpsTypeProvider.Delete(GpsTypeEntity);
            }
            catch (Exception ex)
            {
                HiddenFieldError.Value = ClassManager.CustomExceptionManager.Generate_Error(ex.Message);
                showErrorMessage("No se puede eliminar el registro porque el tipo porque se encuentra asociados a otros registros.");
            }
        }

        protected void lbClearFilters_Click(object sender, EventArgs e)
        {
            GPSTypeGrid.MasterTableView.FilterExpression = string.Empty;

            foreach (GridColumn column in GPSTypeGrid.MasterTableView.RenderColumns)
            {
                if (column is GridBoundColumn)
                {
                    GridBoundColumn boundColumn = column as GridBoundColumn;
                    boundColumn.CurrentFilterValue = string.Empty;
                }
            }


            GPSTypeGrid.MasterTableView.Rebind();
        }

        public void showErrorMessage(string msg)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "objWindowsMessageManager.show_miniError", "objWindowsMessageManager.show_miniError('" + msg + "','Tipos dispositivos...');", true);
        }

        protected void lbShowError_Click(object sender, EventArgs e)
        {
            showErrorMessage(HiddenFieldError.Value);
        }
    }
}